﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gpsys.Entity.MSG;
using Gpsys.Web.Business;
using System.Web.Mvc;

namespace Gpsys.Web.Models
{
    public class TransformEntityModel
    {
        public Transform Transform { get; set; }
    }

    public class TransformModel
    {
        public bool InsertTransform(Transform tr)
        {
            TransformBusiness bs = new TransformBusiness();

            return bs.InsertTransform(tr);
        }

        public bool UpdateTransform(Transform tr)
        {
            TransformBusiness bs = new TransformBusiness();

            return bs.UpdateTransform(tr);
        }

        public Transform GetTransform(int Id)
        {
            TransformBusiness bs = new TransformBusiness();

            return bs.GetTransform(Id);
        }

        public void DeleteTransform(int Id)
        {
            TransformBusiness bs = new TransformBusiness();

            bs.DeleteTransform(Id);
        }

        private List<Transform> m_Transforms;

        public List<Transform> Transforms
        {
            get
            {
                if (m_Transforms == null)
                {
                    TransformBusiness bs = new TransformBusiness();
                    m_Transforms = bs.GetTransforms();
                }
                return m_Transforms;
            }
        }

        public void ValidateTransform(ModelStateDictionary modelState, TransformEntityModel model)
        {
            if (string.IsNullOrEmpty(model.Transform.Code))
                modelState.AddModelError("Transform.Code", "Code could not be empty.");

            if (string.IsNullOrEmpty(model.Transform.Xml))
                modelState.AddModelError("Transform.Xml", "Xml could not be empty.");

            TransformBusiness bs = new TransformBusiness();
            if (bs.CheckCodeUsed(model.Transform))
            {
                modelState.AddModelError("Transform.Code", "Code is already been used.");
            }
        }
    }
}